var table;
var element;
var $;
var layer;
layui.use(['table','element','layer'],function() {
    table = layui.table;
    element = layui.element;
    $ = layui.jquery;
    layer=layui.layer;
    //评论显示条数
    var num=3;
    //评论长度
    var commentNum=0;
    //查看更多评论
    var hideNum=0;
    //页面刷新
    $("#btn_1").on('click',function(){
        location.reload(true);
    });

    //获取当前登陆人员的信息
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    $("#input_id").val(create.true_name);
    $("#input_id1").val(create.account);


    var id=$("#forumId").val();
    getInformation(id);
    function getInformation(id){
        $.ajax({
            url:"/SyteacherSelController/SeUserComment",
            type:"post",
            dataType:"json",
            data:{"id":id},
            success:function(data){
                $("#div_id3").append("<img id='imgId' src='http://182.92.209.121:/"+data.data.avatar_url+"'>");
                $("#p_name").text(data.data.username);
                $("#span_title").text(data.data.title);
                $("#span_user").text(data.data.username);
                $("#span_fenlei").text(data.data.fenlei);
                $("#span_date").text(resolvingDate(data.data.date));
                $("#span_id").text(data.userid);
            }
        });
    }



    seTab(id);
    /*查询表结构开始*/
    function seTab(id){
        $.ajax({
            url:"/SyteacherSelController/seForumTab",
            type:"post",
            dataType:"json",
            data:{"fid":id},
            success:function(data){
                $("#text_id").text(data.data[0].articleContent);
                $("#titleid").text(data.data[0].articleTitle);
                if(data.data[0].articleUrl!=null){
                    $("#img_id").html("<img src='http://182.92.209.121:/"+data.data[0].articleUrl+"' style='width: 520px;height: 500px;'>");
                }
                TabView(data.data);
            }
        })
    }
    /*查询表结构结束*/

    /*渲染表开始*/
    function TabView(da){
        table.render({
            elem:"#tab_id",
            data:da,
            width:500,
            cols:[[
                {field:'articleHits',title:'查看量'},
                {field:'articleLike',title:'点赞数'},
                {field:'articleShare',title:'分享量'},
                {field:'articleHits',title:'收藏'}

            ]]
        })
    }
    /*渲染表结束*/

    /*时间转换*/
    function resolvingDate(date){
        var d = new Date(date);
        var month = (d.getMonth() + 1) < 10 ? '0'+(d.getMonth() + 1) : (d.getMonth() + 1);
        var day = d.getDate()<10 ? '0'+d.getDate() : d.getDate();
        var hours = d.getHours()<10 ? '0'+d.getHours() : d.getHours();
        var min = d.getMinutes()<10 ? '0'+d.getMinutes() : d.getMinutes();
        var sec = d.getSeconds()<10 ? '0'+d.getSeconds() : d.getSeconds();
        times=d.getFullYear() + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + sec;
        return times;
    };

    /*评论信息*/
    seComment(id,num,0);
    function seComment(id,limit,num){
        $.ajax({
            url:"/SyteacherSelController/seComment",
            type:"post",
            dataType:"json",
            data:{"forumid":id,"limitnum":limit},
            success:function(data){
                if(num==1){
                    hideNum+=data.data.length;
                }else{
                    commentNum+=data.data.length;
                }
                var html="";
                for(var a=0;a<data.data.length;a++){
                    html+=
                        "<div class='contentdiv'>" +
                        "<div class='contentdiv1'>" +
                        "<img id='imgId2' src='http://182.92.209.121:/"+data.data[a].avatar_url+"'>" +
                        "</div>" +
                        "<div class='singlediv'>" +
                        "<p class='nameclass'>"+data.data[a].userName+"</p>" +
                        "<p class='nameclass'>回复网友:"+data.data[a].content+"</p>" +
                        "<p class='timeclass'>"+resolvingDate(data.data[a].comment_date)+"</p>" +
                        "<div class='btnTwo'>" +
                        "<a class='a_class'><span onclick='javascript:hideComment(\""+data.data[a].id+"\")'>隐藏</span></a>&nbsp" +
                        "<a href='#' class='a_class' )'><span onclick='javascript:delectComment(\""+data.data[a].id+"\")'>删除</span></a>" +
                        "</div>" +
                        "</div>" +
                        "</div>";
                }
                if(hideNum!=commentNum){
                    $("#comment_id").html(html);
                }else{
                    layer.msg("已无更多评论");
                }
            }
        });


    }

    //查看更多评论
    $("#comment_a").on("click",function(){
        var num2=num+3;
        //查看更多评论
        var hideNum=0;
        seComment(id,num2,1);
    });

    //添加评论
    $("#btn_2").on("click",function(){
        var id=$("#forumId").val();
        var title=$("#text_div1").val();
        if(!title){
            layer.msg("内容不能为空",{icon:5});
        }else{
            $.ajax({
                url:"/SyOperationController/addCommentInfo",
                type:"post",
                dataType:"json",
                data:{"userId":create.user_id,"commodityId":id,"type":2,"content":title},
                success:function(data){
                    if(data.data){
                        location.reload(true);
                    }
                }
            });
        }
    });
});

/*删除评论*/
function delectComment(id){
    $.ajax({
        url:"/SyteacherSelController/deComment",
        type:"post",
        dataType:"json",
        data:{"cid":id},
        success:function(data){
            if(data.data){
                location.reload(true);
            }
        }
    })
}

//隐藏评论
function hideComment(id){
    $.ajax({
        url:"/SyteacherSelController/hideComment",
        type:"post",
        dataType:"json",
        data:{"commentid":id},
        success:function(data){
            if(data){
                location.reload(true);
            }
        }
    });
}


